package com.feng.study.demo.stream;

import com.feng.study.model.Cart;
import com.feng.study.model.Product;
import com.google.common.collect.Lists;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Zeng ShaoYong
 * @Date: 2022-03-11
 * @Description:
 */
public class StreamDemo {
    public static void main(String[] args) {
        Product product1=new Product();
        product1.setProductId("01").setPrice("11");
        Product product2=new Product();
        product2.setProductId("02").setPrice("22");
        Product product3=new Product();
        product3.setProductId("02").setPrice("22");
        Product product4=new Product();
        product4.setProductId("02").setPrice("22");
        Cart cart1 = new Cart();
        cart1.setCartId("001").setAllNumber(3).setTotalPrice("49").setProductList(Lists.newArrayList(product1,product2));
        Cart cart2 = new Cart();
        cart2.setCartId("002").setAllNumber(3).setTotalPrice("99").setProductList(Lists.newArrayList(product3,product4));
        List<Cart> carts= Lists.newArrayList(cart1,cart2);

        //flatmap()
        List<Product> products = carts.stream().map(Cart::getProductList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
        System.out.println(products);
    }
}
